<?php
namespace App\Controller;

use Common\Util\ExcelOut;

class OverwatchStatisController extends OverwatchController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    public function statis() {

        $service = D('Video', 'Service');

        $map['title'] = new \MongoRegex("/^{$this->prefix}/i");
        $map['status'] = $service::STATUS_PASS;
        $map['type'] = $service::TYPE_NORMAL;
        $map['race_id'] = ['$eq' => null];
        $map['game_id'] = $this->game_id;
        if (!empty($this->start) && !empty($this->end)) {
            $map['create_time'] = ['$gte' => $this->start, '$lte' => $this->end];
        }
        $res = $service->where($map)->field('id')->select();

        foreach ($res as $value) {
            $ids[] = $value['id'];
        }
        $likeService = service('Like');
        $condition = [
            'create_time' => ['$gte' => $this->start, '$lte' => $this->end],
            'target_id'   => ['$in' => $ids],
            'type'        => $likeService::TYPE_VIDEO
        ];
        $users = $likeService->where($condition)->field('uid')->select();
        $l = [];
        foreach ($users as $value) {
            if ($l[$value['uid']]) {
                $l[$value['uid']] ++;
            } else {
                $l[$value['uid']] = 1;
            }
        }

        $list = [];
        $userEvent = event('user');
        foreach ($l as $key => $value) {
            if ($key) {
                $user = $userEvent->info($key, true);
                $list[] = [
                    'nickname' => $user['nickname'],
                    'reg_time' => date('Y/m/d H:i:s', $user['reg_time']),
                    'id'       => $key,
                    'like'      => $value
                ];
                $reg_time = date('Y/m/d H:i:s', $user['reg_time']);
                $array[] = [
                    "{$key}",
                    "{$user['nickname']}",
                    "{$value}",
                    "{$reg_time}",
                ];
            } else {
                $list[] = [
                    'nickname' => '机器人',
                    'reg_time' => '机器人',
                    'id'       => 0,
                    'like'      => $value
                ];
                // echo "0*66机器人*{$value}*66机器人<br>";
            }
        }
        $ExcelOut = new ExcelOut('utf8', "守望先锋【重装】");
        $ExcelOut->add_array($array);
        $ExcelOut->output();
        exit;
        echo "<br><br>";
        echo "参与总人数：".count($l);
        exit;




        $list = [];
        $likeService = service('Like');
        $vEvent = event('video');
        foreach ($res as $value) {
            $vid = $value['id'];

            $condition = [
                'create_time' => ['$gte' => $this->start, '$lte' => $this->end],
                'target_id'   => $vid,
                'type'        => $likeService::TYPE_VIDEO
            ];
            $video = $vEvent->detail($vid);
            $video['zan'] = $likeService->where($condition)->count();

            $list[] = $video;
        }

        foreach ($list as $key => $value) {
            $likeCount[$key] = $value['zan'];
        }

        array_multisort($likeCount, SORT_DESC, $list);

        foreach ($list as $video) {
            $time = date('Y/m/d H:i:s', $video['create_time']);
            $url = 'http://www.66play.com'.U('home/share/video', ['id' => $video['id']]);
            echo "{$video['id']}*{$video['zan']}*{$video['title']}*{$video['user']['nickname']}*{$time}*{$url}<br>";
        }
    }
}